// import { ACCESS_TOKEN } from "@/store/mutation-types";
import axios from "axios";
// import store from '@/store'
// import storage from 'store'
import { VueAxios } from "./axios";
import tool from "./tool";
// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: process.env.VUE_APP_API_BASE_URL,
  // baseURL: config.baseURL.target,

  timeout: 6000 // 请求超时时间
});

// 异常拦截处理器
const errorHandler = error => {
  if (error.response) {
    const data = error.response.data.message;

    // 从 localstorage 获取 token
    // const token = storage.get(ACCESS_TOKEN)
    const token = true;
    if (error.response.status == 400) {
      // this.$message.error(error.response.data.message)
      Toast(error.response.data.message);
    }
    if (error.response.status === 403) {
      Toast(data);
      // notification.error({
      //   message: 'Forbidden',
      //   description: data
      // })
    }
    if (
      error.response.status === 401 &&
      !(data.result && data.result.isLogin)
    ) {
      Toast("Authorization verification failed");
      // notification.error({
      //   message: 'Unauthorized',
      //   description: 'Authorization verification failed'
      // })
      if (token) {
        store.dispatch("Logout").then(() => {
          setTimeout(() => {
            window.location.reload();
          }, 1500);
        });
      }
    }
  }
  return Promise.reject(error);
};

// request interceptor
request.interceptors.request.use(config => {
  const token = window.localStorage.getItem("token");
  if (token) {
    config.headers["Authorization"] = `${token}`;
  } else {
    window.location.href = "/#/login";
  }

  return config;
}, errorHandler);

// response interceptor
request.interceptors.response.use(
  response => {
    return response.data;
  },
  err => {
    if (err && err.response) {
      let msg = err.response.data.message;
      switch (err.response.status) {
        case 400:
          err.message = msg;
          break;

        case 401:
          // Dialog.alert({
          //   title: "请求报错",
          //   message: "未授权，请登录"
          // }).then(() => {
          //   // let name = window.location.hash.split("#/")[1] || "";
          //   window.location.href = "/#/login?topath=" + name;
          // });
          window.location.href = "/#/login";
          break;

        case 403:
          // err.message = "拒绝访问";
          err.message = msg;
          break;

        case 404:
          // err.message = `请求地址出错: ${err.response.config.url}`;
          err.message = msg;

          break;

        case 408:
          // err.message = "请求超时";
          err.message = msg;

          break;

        case 500:
          // err.message = "服务器内部错误";
          err.message = msg;

          break;

        case 501:
          // err.message = "服务未实现";
          err.message = msg;

          break;

        case 502:
          // err.message = "网关错误";
          err.message = msg;

          break;

        case 503:
          // err.message = "服务不可用";
          err.message = msg;

          break;

        case 504:
          // err.message = "网关超时";
          err.message = msg;

          break;

        case 505:
          // err.message = "HTTP版本不受支持";
          err.message = msg;

          break;

        default:
      }
    }
    if (err.message == "Network Error") {
      //网络出现问题
      console.log("网络异常 请切换网络环境");
    } else {
      return Promise.reject(err); // 返回接口返回的错误信息
    }
  }
);

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, request);
  }
};

export default request;

export { installer as VueAxios, request as axios };